package com.kinghood.productcenter.service.base.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.kinghood.infrastructure.model.constant.CommonConstants;
import com.kinghood.infrastructure.util.UserUtil;
import com.kinghood.infrastructure.util.base.IdGenUtil;
import com.kinghood.productcenter.common.model.dao.base.BaseAttributeDO;
import com.kinghood.productcenter.common.model.dao.base.BaseProductTemplateGitemDO;
import com.kinghood.productcenter.common.model.dao.base.BaseProductTemplateGroupDO;
import com.kinghood.productcenter.common.model.dao.base.BaseProductTemplateRuleDO;
import com.kinghood.productcenter.common.model.dto.base.GetNodeDTO;
import com.kinghood.productcenter.common.model.dto.product.ProductTemplateNodeDTO;
import com.kinghood.productcenter.dao.base.BaseAttributeMapper;
import com.kinghood.productcenter.dao.base.BaseProductTemplateGroupMapper;
import com.kinghood.productcenter.service.base.IBaseProductTemplateGroupService;
import com.kinghood.productcenter.service.base.handler.ProductTemplateHandler;
import lombok.RequiredArgsConstructor;
import org.apache.xmlbeans.impl.xb.xmlschema.BaseAttribute;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 基础选项-模板分组 服务实现类
 * </p>
 *
 * @author KingHood
 * @since 2023-12-20
 */
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
@Service
public class BaseProductTemplateGroupServiceImpl extends ServiceImpl<BaseProductTemplateGroupMapper, BaseProductTemplateGroupDO> implements IBaseProductTemplateGroupService {

    private final BaseAttributeMapper attributeMapper;
    private final BaseProductTemplateGitemServiceImpl templateGitemService;
    private final BaseProductTemplateRuleServiceImpl templateRuleService;

    @Override
    public void addTemplate(Long productTemplateId, List<ProductTemplateNodeDTO> nodeList) {

        if (CollectionUtils.isEmpty(nodeList)) {
            return;
        }
        List<BaseAttributeDO> attributeDOList = new LambdaQueryChainWrapper<>(attributeMapper)
                .eq(BaseAttributeDO::getIsDelete, CommonConstants.IS_DELETE_N)
                .list();
        Map<Long, BaseAttributeDO> attributeIdMap = attributeDOList.stream()
                .collect(Collectors.toMap(x -> x.getId(), v -> v, (k1, k2) -> k1));

        GetNodeDTO param = new GetNodeDTO();
        param.setProductTemplateId(productTemplateId);
        param.setAttributeIdMap(attributeIdMap);
        ProductTemplateHandler.getNode(param, null, nodeList, "");

        this.saveBatch(param.getList());
        templateGitemService.saveBatch(param.getItemList());
        templateRuleService.saveBatch(param.getRuleList());
    }


}
